package com.example.membersteward.controller;

import com.example.membersteward.common.CommonResult;
import com.example.membersteward.entity.Menu;
import com.example.membersteward.service.MenuService;
import com.example.membersteward.service.RoleUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class MenuController {
    @Resource
    MenuService menuService;

    @Resource
    RoleUserService roleUserService;


    @GetMapping("/getMenu")
    public CommonResult getMenu(){
        return menuService.getMenu();
    }

    @GetMapping("/getAllMenu")
    public CommonResult getAllMenu() {
        return menuService.getAllMenu();
    }

    @GetMapping("/getMenuById")
    public CommonResult getMenuById(Long menuId) {
        return menuService.getMenuById(menuId);
    }
    @GetMapping("/deleteMenu")
    public CommonResult deleteMenu(Long menuId){
        CommonResult commonResult = roleUserService.isAdmin();
        if (commonResult.isError()) {
            return commonResult;
        }
        return menuService.deleteMenu(menuId);
    }

    @PostMapping("/insertMenu")
    public CommonResult insertMenu(@RequestBody Menu menu){
        CommonResult commonResult = roleUserService.isAdmin();
        if (commonResult.isError()) {
            return commonResult;
        }
        return menuService.insertMenu(menu);
    }

    @PostMapping("/updateMenu")
    public CommonResult updateMenu(@RequestBody Menu menu){
        CommonResult commonResult = roleUserService.isAdmin();
        if (commonResult.isError()) {
            return commonResult;
        }
        return menuService.updateMenu(menu);
    }
}
